package com.hj.saapp.common.utils;


import android.os.AsyncTask;

import com.google.gson.Gson;
import com.google.gson.JsonObject;
import com.hj.saapp.BCDApplication;
import com.hj.saapp.bean.CaseDataBean;
import com.hj.saapp.bean.LoginDataBean;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.HashMap;
import java.util.Map;

import lib.hj.com.commonlib.bean.BaseBean;
import lib.hj.com.commonlib.manager.HttpManager;
import lib.hj.com.commonlib.utils.Constant;
import lib.hj.com.commonlib.utils.OnAsyncHttpResponse;
import lib.hj.com.commonlib.utils.Req;





/**
 * author:dyz
 * 网络请求业务层
 * Created by Cola on 15/10/8.
 */

public class HttpHelper {



    //不带参数
    public  static  void  getData(Class cls,OnAsyncHttpResponse asynHttp) {

        Req req = new Req();
        req.cls = cls;
        req.onAsynHttp = asynHttp;
        req.url = "http://baike.baidu.com/api/openapi/BaikeLemmaCardApi?scope=103&format=json&appid=379020&bk_key=%E9%93%B6%E9%AD%82&bk_length=600";
        HttpManager.getHttpData(req);

    }

    //带参数
    public  static  void  getData2(Class cls, Map<String,String>map,OnAsyncHttpResponse asynHttp) {

        Req req = new Req();
        req.cls = cls;
        //参数
//        req.parameter = map;
        req.onAsynHttp = asynHttp;
        req.url = "http://test.heimaoyc.com/heimao/shipperApp.shtml";
        HttpManager.getHttpData(req);

    }

    //用户信息查询
    public static void getUserInfo(OnAsyncHttpResponse asynHttp,JsonObject json) {

        Map <String,String>map = new HashMap<>();
        map.put("data", new Gson().toJson(json));
        Req req = new Req();
        req.cls = LoginDataBean.class;
        req.parameter = map;
        req.onAsynHttp = asynHttp;
        req.url = BCDApplication.Service_addr+ Constant.getUserInfo;
        req.method= Constant.Method.POST;
        HttpManager.getHttpData(req);

    }


    //用户信息查询
    public static void login(OnAsyncHttpResponse asynHttp,JsonObject json) {



        Map <String,String>map = new HashMap<>();
        map.put("data", new Gson().toJson(json));
        Req req = new Req();
        req.cls = LoginDataBean.class;
        req.parameter = map;
        req.onAsynHttp = asynHttp;
        req.url = Constant.login;
        req.method= Constant.Method.POST;
        HttpManager.getHttpData(req);

    }

    //案件查询
    public static void searchCase(OnAsyncHttpResponse asynHttp,Map map) {


        Req req = new Req();
        req.cls = CaseDataBean.class;
        req.parameter = map;
        req.onAsynHttp = asynHttp;
        req.url = BCDApplication.Service_addr+ Constant.searchCase;
        req.method= Constant.Method.POST;
        HttpManager.getHttpData(req);

    }








    //案件查询
    public static void getFinancialInfo(OnAsyncHttpResponse asynHttp) {

        Map<String, String> map = new HashMap<>();
        String str1 = "非法入境";
        String str = "{\"AJBH\":\"\",\"AJLX\":\"\",\"AJMC\":\"\\u0025单位秩序\\u0025\",\"BEGINTIME\":\"\",\"ENDTIME\":\"\"}";
        String log = "{\"DEPARTMENTCODE\":\"440000260200\",\"USER_ID\":\"003223\",\"USER_NAME\":\"蔡妍珊\"}";
        map.put("log", log);
        map.put("data", str);
        Req req = new Req();
        req.cls = BaseBean.class;
        req.parameter = map;
        req.onAsynHttp = asynHttp;
        req.url = "http://127.0.0.1:16789/sagl/basic/services/searchService/aj/get.action";
        req.method= Constant.Method.POST;
        HttpManager.getHttpData(req);
    }
    //财物查询
    public static void getPropertyInfo(OnAsyncHttpResponse asynHttp) {


        Map<String, String> map = new HashMap<>();
        String str = "{\"cw\":{\"AJBH\":\"\",\"BEGINTIME\":\"2012-10-8 00:00:00\",\"ENDTIME\":\"\",\"RESERVATION06\":\"\",\"RESERVER79\":\"\",\"WPBH\":\"\"},\"log\":{\"DEPARTMENTCODE\":\"440000260200\",\"USER_ID\":\"003223\",\"USER_NAME\":\"蔡妍珊\"}}";
        map.put("data", str);
        Req req = new Req();
        req.cls = BaseBean.class;
//        req.parameter = map;
        req.onAsynHttp = asynHttp;
        req.url = "http://127.0.0.1:16789/sagl/basic/services/searchService/cwcx/get.action";
        req.method= Constant.Method.POST;
        HttpManager.getHttpData(req);
    }

    //用户信息查询
    public static void testPost(OnAsyncHttpResponse asynHttp) {


        Map<String, String> map = new HashMap<>();
        map.put("method", "getTruckType");


        Req req = new Req();
        req.cls = BaseBean.class;
//        req.parameter = map;
        req.onAsynHttp = asynHttp;
        req.url = "http://test.heimaoyc.com/heimao/shipperApp.shtml";
        req.method= Constant.Method.POST;

        HttpManager.getHttpData(req);


    }

    //用户信息查询
    public static void testURLPost(final OnAsyncHttpResponse asynHttp) {

        AsyncTask asyncTask = new AsyncTask() {
            @Override
            protected Object doInBackground(Object[] params) {

                BaseBean baseBean = new BaseBean();

                String wen = "MS2201828";
                String btnSearch = "EMS快递查询";
                URL url = null;
                try {
                    url = new URL("http://192.168.8.73:8080/heimao/shipperApp.shtml?method=getTruckType&json={}");
                    HttpURLConnection conn = (HttpURLConnection) url.openConnection();
                    conn.setRequestMethod("POST");// 提交模式
                    // conn.setConnectTimeout(10000);//连接超时 单位毫秒
                    // conn.setReadTimeout(2000);//读取超时 单位毫秒
                    conn.setDoOutput(true);// 是否输入参数

                    StringBuffer params1 = new StringBuffer();
                    // 表单参数与get形式一样
                    params1.append("wen").append("=").append(wen).append("&")
                            .append("btnSearch").append("=").append(btnSearch);
                    byte[] bypes = params.toString().getBytes();
                    conn.getOutputStream().write(bypes);// 输入参数
                    InputStream inStream=conn.getInputStream();
                    BufferedReader reader = new BufferedReader(new InputStreamReader(inStream));
                    StringBuilder sb = new StringBuilder();
                    String line = null;
                    try {

                        while ((line = reader.readLine()) != null) {
                            sb.append(line + "/n");
                        }
                        baseBean.setUrl(url.toString());
                        baseBean.setResult(sb.toString());


                    } catch (IOException e) {
                        e.printStackTrace();

                    } finally {
                        try {
                            inStream.close();
                        } catch (IOException e) {
                            e.printStackTrace();
                        }

                    }

                } catch (Exception e) {
                    e.printStackTrace();
                }

                return baseBean;
            }

            @Override
            protected void onPostExecute(Object o) {
                asynHttp.onSuccess((BaseBean) o);
                super.onPostExecute(o);
            }
        };

        asyncTask.execute();



    }

//    //
//    public static void postPt(OnAsyncHttpResponse asynHttp,Map map) {
//
//        Req req = new Req();
//        req.cls = PostPtBean.class;
//        req.parameter = map;
//        req.onAsynHttp = asynHttp;
////        req.url = BCDApplication.Service_addr+ Constant.savephoto;
//        req.url = BCDApplication.Service_addr+ Constant.savephoto;
//        req.method= Constant.Method.POST;
//        HttpManager.getHttpData(req);
//    }



}
